package com.atguigu.gmall2023sugar.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall2023sugar.bean.*;
import com.atguigu.gmall2023sugar.service.TradeService;
import com.atguigu.gmall2023sugar.service.TrafficService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lzc
 * @Date 2023/7/18 10:41
 */
@RestController
public class SugarController {
    
    @Autowired
    TradeService tradeService;
    
    @RequestMapping("/sugar/trade/gmv")
    public String gmv(String date) {
        if (date == null) {
            date = new SimpleDateFormat("yyyyMMdd").format(new Date());
        }
        
        BigDecimal gmv = tradeService.gmv(date);
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        result.put("data", gmv);
        
        return result.toJSONString();
    }
    
    @RequestMapping("/sugar/trade/gmv/tm")
    public String gmvByTm(String date, Integer top) {
        if (date == null) {
            date = new SimpleDateFormat("yyyyMMdd").format(new Date());
        }
        
        if (top == null) {
            top = 5;
        }
        
        List<Tm> list = tradeService.gmvByTm(date, top);
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray categories = new JSONArray();
        for (Tm tm : list) {
            categories.add(tm.getTrademark_name());
        }
        data.put("categories", categories);
        
        JSONArray series = new JSONArray();
        JSONObject obj = new JSONObject();
        obj.put("name", "商品品牌");
        JSONArray innerData = new JSONArray();
        for (Tm tm : list) {
            innerData.add(tm.getOrder_amount());
        }
        obj.put("data", innerData);
        series.add(obj);
        data.put("series", series);
        
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    
    @RequestMapping("/sugar/trade/gmv/c3")
    public String gmvByC3(String date, Integer top) {
        if (date == null) {
            date = new SimpleDateFormat("yyyyMMdd").format(new Date());
        }
        
        if (top == null) {
            top = 5;
        }
        
        List<C3> list = tradeService.gmvByC3(date, top);
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONArray data = new JSONArray();
        for (C3 c3 : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", c3.getCategory3_name());
            obj.put("value", c3.getOrder_amount());
            data.add(obj);
        }
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    
    @Autowired
    TrafficService trafficService;
    
    @RequestMapping("/sugar/traffic")
    public String traffic(String date) {
        if (date == null) {
            date = new SimpleDateFormat("yyyyMMdd").format(new Date());
        }
        
        List<Traffic> list = trafficService.traffic(date);
        
        /// 把 list 转存入到 map 中,方便后面的读取
        Map<Integer, Traffic> hourToTraffic = new HashMap<>();
        for (Traffic traffic : list) {
            hourToTraffic.put(traffic.getHr(), traffic);
        }
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray categories = new JSONArray();
        for (int hour = 0; hour < 24; hour++) {
            categories.add(hour);
        }
        data.put("categories", categories);
        
        JSONArray series = new JSONArray();
        
        JSONObject pv = new JSONObject();
        pv.put("name", "pv");
        JSONArray pvData = new JSONArray();
        pv.put("data", pvData);
        series.add(pv);
        
        JSONObject uv = new JSONObject();
        uv.put("name", "uv");
        JSONArray uvData = new JSONArray();
        uv.put("data", uvData);
        series.add(uv);
        
        JSONObject sv = new JSONObject();
        sv.put("name", "sv");
        JSONArray svData = new JSONArray();
        sv.put("data", svData);
        series.add(sv);
        
        for (int hour = 0; hour < 24; hour++) {
            Traffic traffic = hourToTraffic.getOrDefault(hour, new Traffic(hour, 0L, 0L, 0L));
            pvData.add(traffic.getPv());
            uvData.add(traffic.getUv());
            svData.add(traffic.getSv());
        }
        
        data.put("series", series);
        
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    
    @RequestMapping("/sugar/province")
    public String province(String date) {
        if (date == null) {
            date = new SimpleDateFormat("yyyyMMdd").format(new Date());
        }
        
        List<Province> list = tradeService.province(date);
        
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        data.put("valueName", "销售额");
        
        JSONArray tooltipNames = new JSONArray();
        tooltipNames.add("订单数");
        data.put("tooltipNames", tooltipNames);
        
        JSONArray tooltipUnits = new JSONArray();
        tooltipUnits.add("个");
        data.put("tooltipUnits", tooltipUnits);
        
        JSONArray mapData = new JSONArray();
        for (Province province : list) {
            JSONObject obj = new JSONObject();
            
            obj.put("name", province.getProvince_name());
            obj.put("value", province.getOrder_amount());
            
            JSONArray tooltipValues = new JSONArray();
            tooltipValues.add(province.getOrder_count());
            obj.put("tooltipValues", tooltipValues);
            
            mapData.add(obj);
        }
        
        data.put("mapData", mapData);
        
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    @RequestMapping("/sugar/trade/order_and_refund")
    public String orderAndRefund(String date) {
        if (date == null) {
            date = new SimpleDateFormat("yyyyMMdd").format(new Date());
        }
        
        List<OrderAndRefund> list = tradeService.orderAndRefund(date);
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        JSONArray columns = new JSONArray();
        
        JSONObject c1 = new JSONObject();
        c1.put("name", "商品品牌");
        c1.put("id", "tm");
        columns.add(c1);
        
        JSONObject c2 = new JSONObject();
        c2.put("name", "下单金额");
        c2.put("id", "order_amount");
        columns.add(c2);
        
        JSONObject c3 = new JSONObject();
        c3.put("name", "退单数");
        c3.put("id", "refund_count");
        columns.add(c3);
        
        JSONObject c4 = new JSONObject();
        c4.put("name", "退单人数");
        c4.put("id", "refund_user_count");
        columns.add(c4);
        
        data.put("columns", columns);
    
        JSONArray rows = new JSONArray();
        for (OrderAndRefund or : list) {
            JSONObject row = new JSONObject();
            row.put("tm", or.getTrademark_name());
            row.put("order_amount", or.getOrder_amount());
            row.put("refund_count", or.getRefund_count());
            row.put("refund_user_count", or.getRefund_user_count());
            rows.add(row);
        }
    
        data.put("rows", rows);
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    @RequestMapping("/sugar/traffic/kw")
    public String kw(String date) {
        if (date == null) {
            date = new SimpleDateFormat("yyyyMMdd").format(new Date());
        }
    
        Map<String, Long> map = trafficService.kw(date);
    
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
    
        JSONArray data = new JSONArray();
    
        for (String name : map.keySet()) {
            Long value = map.get(name);
    
            JSONObject obj = new JSONObject();
            obj.put("name", name);
            obj.put("value", value);
    
            data.add(obj);
        }
        
        result.put("data", data);
        
        return result.toJSONString();
    }
}


